package com.dure.hotmemory.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dure.commonlib.util.AssetsUtil;
import com.dure.hotmemory.MainActivity;
import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.Code;
import com.dure.hotmemory.bean.CodeDao;
import com.dure.hotmemory.listeners.TopChangeListener;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by dure on 2017/5/31.
 */

public class CodeDetailAdapter extends RecyclerView.Adapter<CodeDetailAdapter.MyViewHolder> {
    List<Code> codeList = new ArrayList<>();
    Context context;
    MainActivity activity;
    TopChangeListener topChangeListener;

    public CodeDetailAdapter(Context context) {
        this.context = context;
        activity = (MainActivity) context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.code_detail, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final Code code = codeList.get(position);
        holder.nameTV.setText(code.name);
        holder.numberTV.setText(code.number);

        Bitmap bitmap = null;
        if (!code.isCreated) {
            bitmap = BitmapFactory.decodeStream(AssetsUtil.getAssets(context, code.image));
        } else {
            FileInputStream is = null;
            try {
                is = new FileInputStream(code.image);
                bitmap = BitmapFactory.decodeFileDescriptor(is.getFD(), null, null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bitmap != null)
            holder.codeIV.setImageBitmap(bitmap);

        if (position == 0) {
            holder.topIV.setVisibility(View.GONE);

            holder.primeTV.setBackgroundResource(R.drawable.prime_green_drawable);
            holder.primeTV.setText("首选");
        } else {
            holder.topIV.setVisibility(View.VISIBLE);
            holder.primeTV.setBackgroundResource(R.drawable.prime_gray_drawable);
            holder.primeTV.setText("");
        }

        holder.topIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                moveToFirst(code);
            }
        });
    }

    private void moveToFirst(Code code) {
        int originIndex = this.codeList.indexOf(code);
        Code firstCode = codeList.get(0);
//        this.codeList.remove(code);
//        this.codeList.add(0, code);
        Collections.swap(codeList, 0, originIndex);


        CodeDao codeDao = activity.daoSession.getCodeDao();
        firstCode.position = code.position;
        codeDao.update(firstCode);
        code.position = 0;
        codeDao.update(code);


        notifyItemMoved(originIndex, 0);
//        notifyDataSetChanged();
//        notifyItemChanged(originIndex);
        notifyItemRangeChanged(0, originIndex + 1);

        if (topChangeListener != null){
            topChangeListener.onTopChanged(code);
        }
    }

    public void setTopChangeListener(TopChangeListener topChangeListener) {
        this.topChangeListener = topChangeListener;
    }

    @Override
    public int getItemCount() {
        return codeList.size();
    }

    public void setCodeList(List<Code> codeList) {
        if (codeList != null && codeList.size() > 0) {
            this.codeList.addAll(codeList);
            notifyDataSetChanged();
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView numberTV, primeTV, nameTV;
        ImageView codeIV, topIV;

        public MyViewHolder(View view) {
            super(view);
            nameTV = (TextView) view.findViewById(R.id.name_tv);
            numberTV = (TextView) view.findViewById(R.id.number_tv);
            primeTV = (TextView) view.findViewById(R.id.prime_tv);
            codeIV = (ImageView) view.findViewById(R.id.code_iv);
            topIV = (ImageView) view.findViewById(R.id.top_iv);
        }
    }
}
